
const AccountDefault = () => {
    //格式化后的模板数据
    return {
        name:'',
        permissions:{
            owner : 'EOS8SFR2nmePUrbyGnE4rRd4ZtwTXkTyWuczn25umyMafvx8XbmcJ',
            active : 'EOS8SFR2nmePUrbyGnE4rRd4ZtwTXkTyWuczn25umyMafvx8XbmcJ',
        },
        balance:{
            liquid:         '2.9000 EOS',
            staked:         '1.0000 EOS',
            unstaking:      '0.0000 EOS',
            total:          '3.9000 EOS',
        },
        ram:{
            quota:              '3.979 KiB',
            used:               '3.498 KiB',
            price:              0.0000,
            usedRate:           '0.00%',
            availableRate:      '0.00%',
            canBuy:             '0.00 KiB',
            canSell:            '0.00 EOS',
        },
        net:{
            staked:          '0.5000 EOS ',          //(total stake delegated from account to self)
            unstaking:       '0.0000 EOS',
            delegated:       '0.0000 EOS',           //(total staked delegated to account from others)
            used:               '257 bytes',
            available:        '331.8 KiB',
            limit:              '332 KiB',
            price:              0.0000,
        },
        cpu :{
            staked:          '0.5000 EOS',           //(total stake delegated from account to self)
            unstaking:       '0.0000 EOS',
            delegated:       '0.0000 EOS ',          //(total staked delegated to account from others)
            used:             '6.351 ms',
            available:        '4.923 ms',
            limit:            '11.27 ms',
            price:              0.0000,
        },
        producers:[
            'eosmedinodes'
        ]
    };
};

export const Format = (input) => {
    //输入从eos获取的账户数据、eos价格、内存价格、宽带价格、汇率等
    //获得格式化的数据
    if(!input){
        return input;
    }

    let data = input.data;
    if(!data){
        return data;
    }

    if(!data.account_name){
        return data;
    }

    let account = AccountDefault();

    //name
    account.name = data.account_name;

    //cpu
    account.cpu.staked = EosBalanceFormat(data.total_resources.cpu_weight);
    account.cpu.unstaking = (data.refund_request)?EosBalanceFormat(data.refund_request.cpu_amount) : 0.0000;
    account.cpu.used = (data.cpu_limit.used / 1000).toFixed(2);
    account.cpu.available = (data.cpu_limit.available / 1000).toFixed(2);
    account.cpu.limit = (data.cpu_limit.max / 1000).toFixed(2);
    account.cpu.delegated = EosBalanceFormat(data.self_delegated_bandwidth?data.self_delegated_bandwidth.cpu_weight:'0.0000').toFixed(2);
    account.cpu.total = EosBalanceFormat(data.total_resources?data.total_resources.cpu_weight:'0.0000').toFixed(2);
    account.cpu.price = 0.0179;
    account.cpu.availableRate = ((data.cpu_limit.available / data.cpu_limit.max)*100).toFixed(2);
    account.cpu.delegatedRate = ((account.cpu.delegated / account.cpu.total)*100).toFixed(2);
    account.cpu.refundAmout = EosBalanceFormat(data.refund_request?data.refund_request.cpu_amount:'0.0000');
    account.cpu.refundTime = data.refund_request?data.refund_request.request_time:'00:00:00';
    account.cpu.hadRefund = data.refund_request && data.refund_request.cpu_amount !== '0.0000 EOS';

    //net
    account.net.staked = EosBalanceFormat(data.total_resources.net_weight);
    account.net.unstaking = (data.refund_request)?EosBalanceFormat(data.refund_request.net_amount) : 0.0000;
    account.net.used = (data.net_limit.used / 1024).toFixed(2);
    account.net.available = (data.net_limit.available / 1024).toFixed(2);
    account.net.limit = (data.net_limit.max / 1024).toFixed(2);
    account.net.delegated = EosBalanceFormat(data.self_delegated_bandwidth?data.self_delegated_bandwidth.net_weight:'0.0000').toFixed(2);
    account.net.total = EosBalanceFormat(data.total_resources?data.total_resources.net_weight:'0.0000').toFixed(2);
    account.net.price = 0.0005;
    account.net.availableRate = ((data.net_limit.available / data.net_limit.max)*100).toFixed(2);
    account.net.delegatedRate = ((account.net.delegated / account.net.total)*100).toFixed(2);
    account.net.refundAmout = EosBalanceFormat(data.refund_request?data.refund_request.net_amount:'0.0000');
    account.net.refundTime = data.refund_request?data.refund_request.request_time:'00:00:00';
    account.net.hadRefund = data.refund_request && data.refund_request.net_amount !== '0.0000 EOS';

    //balance
    let chy = 50;
    let usd = chy / 7;
    account.balance.liquid = EosBalanceFormat(data.core_liquid_balance || '0.0000 EOS');
    account.balance.staked = (Number(account.cpu.staked)+Number(account.net.staked)).toFixed(4);
    account.balance.unstaking = (Number(account.cpu.unstaking)+Number(account.net.unstaking)).toFixed(4);
    account.balance.total = Number(account.balance.liquid)+Number(account.balance.staked);
    account.balance.liquidCHY = Number(account.balance.liquid) * chy;
    account.balance.liquidUSD = Number(account.balance.liquid) * usd;

    //ram
    account.ram.quota = ((data.total_resources.ram_bytes) / 1024).toFixed(2);
    account.ram.used = (data.ram_usage / 1024).toFixed(2);
    account.ram.available = ((data.total_resources.ram_bytes - data.ram_usage) / 1024).toFixed(2);
    account.ram.usedRate = ((data.ram_usage / data.total_resources.ram_bytes)*100).toFixed(2);
    account.ram.availableRate = (((data.total_resources.ram_bytes - data.ram_usage) / data.total_resources.ram_bytes)*100).toFixed(2);
    account.ram.price = 0.158720;
    account.ram.canBuy = (Number(account.balance.liquid) / Number(account.ram.price)).toFixed(2);
    account.ram.canSell = (Number(account.ram.available) * Number(account.ram.price)).toFixed(2);

    //permissions
    let perms = data.permissions;
    for(let i=0; i<perms.length; i++){
        if(perms[i].perm_name === 'active'){
            account.permissions.active = perms[i].required_auth.keys[0].key;
        }else if(perms[i].perm_name === 'owner'){
            account.permissions.owner = perms[i].required_auth.keys[0].key;
        }
    }

    //vote
    account.producers = data.voter_info.producers;

    return account;
};

const FormatDes = (value,des) => {
    //符号描述数据，比如0.0001 EOS，331.8 KiB，6.351 ms，100 ￥
    let format = value.toString();
    if(des === 'EOS'){
        format = value + ' EOS';
    }else if(des === 'CPU'){
        format = value + ' ms';
    }else if(des === 'ARM'|| des === 'NET'){
        if(Number(value) > 1){
            format = value + ' KiB';
        }else{
            format = Math.floor(Number(value)*1024) + ' bytes';
        }
    }else if(des === 'RATE'){
        format = value+'%';
    }
    return format;
};


const EosBalanceFormat = (value) => {
    if(!value){
        return 0.0000;
    }
    if(typeof(value) === 'string'){
        value = value.replace(' EOS','');
        let num = Number(value);
        if(typeof(num) === 'number'){
            return num;
        }
    }else if(typeof(value) === 'number'){
        let num = value/10000;
        num = num.toFixed(4);
        return num;
    }
};

//转换时间
// const transferTimeZone = (date) => {
//     // //转换时间
//     return moment(date).add(72, 'hours').format('YYYY-MM-DDTHH:mm:ss');
// };

//时间百分比防出错
const falseAlarm = (timePercentage)=> {
    let Percentage = timePercentage.replace("%", "")
    let newtimePercentage;
    if (Percentage <= 0) {
        newtimePercentage = '0%'
    } else if (Percentage >= 100) {
        newtimePercentage = '100%'
    } else {
        newtimePercentage = timePercentage;
    }
    return newtimePercentage
};

export default {
    Format,
    FormatDes,
    EosBalanceFormat
}


//从EOS服务器来的原始JSON数据
/*
{
    "account_name": "zuowen123123",
    "head_block_num": 11075312,
    "head_block_time": "2018-08-14T11:39:01.000",
    "privileged": false,
    "last_code_update": "1970-01-01T00:00:00.000",
    "created": "2018-06-25T03:50:52.000",
    "ram_quota": 4074,
    "net_weight": 5000,
    "cpu_weight": 5000,
    "net_limit": {
    "used": 257,
        "available": 339738,
        "max": 339995
},
    "cpu_limit": {
    "used": 6351,
        "available": 4710,
        "max": 11061
},
    "ram_usage": 3582,
    "permissions": [{
    "perm_name": "active",
    "parent": "owner",
    "required_auth": {
        "threshold": 1,
        "keys": [{
            "key": "EOS8SFR2nmePUrbyGnE4rRd4ZtwTXkTyWuczn25umyMafvx8XbmcJ",
            "weight": 1
        }
        ],
        "accounts": [],
        "waits": []
    }
},{
    "perm_name": "owner",
    "parent": "",
    "required_auth": {
        "threshold": 1,
        "keys": [{
            "key": "EOS8SFR2nmePUrbyGnE4rRd4ZtwTXkTyWuczn25umyMafvx8XbmcJ",
            "weight": 1
        }
        ],
        "accounts": [],
        "waits": []
    }
}
],
    "total_resources": {
    "owner": "zuowen123123",
        "net_weight": "0.5000 EOS",
        "cpu_weight": "0.5000 EOS",
        "ram_bytes": 4074
},
    "self_delegated_bandwidth": {
    "from": "zuowen123123",
        "to": "zuowen123123",
        "net_weight": "0.5000 EOS",
        "cpu_weight": "0.5000 EOS"
},
    "refund_request": null,
    "voter_info": {
    "owner": "zuowen123123",
        "proxy": "",
        "producers": [
        "eosmedinodes"
    ],
        "staked": 10000,
        "last_vote_weight": "4124461241.63100862503051758",
        "proxied_vote_weight": "0.00000000000000000",
        "is_proxy": 0
}
}*/
